import {defineStore} from 'pinia'


const state = ()=>( {
	index: 1, //自增ID
	list: [], //用户列表
	loginUser: {}, //登录用户信息
	
})

const getters = {

}

const actions = {
	save(data) {
		if(!data.id){
			//新增
			data.id = this.index;
			this.index++;
			this.list.push(data);
		}else{
			//修改
			this.list = this.list.map(item => {
				if (item.id == data.id) {
					return data;
				}
				return item;
			})
		}
		
	},
	del(id) {
		this.list = this.list.filter(item => item.id != id);
	},
}

export const userStore = defineStore({
	id: 'user-store',
	state,
	getters,
	actions,
	persist: { //  持久化插件
	    enabled: true, // 使用存储 
	}

})


